package com.nrgedge.filter;

import info.magnolia.cms.core.AggregationState;
import info.magnolia.context.MgnlContext;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class HardCodedMustacheSubstitutionFilter extends AbstractMustacheSubstitutionFilter {

	private static Map<String, Object> accountDetails = new HashMap<String, Object>();
	private static Map<String, Object> home = new HashMap<String, Object>();
	private static Map<String, Object> login = new HashMap<String, Object>();
	private static final int NUMBER_OF_EXTRA_ACCOUNTS = 3;
	private static final int NUMBER_OF_EXTRA_TRANSACTIONS = 9;
	private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

	static {
		populateAccountDetail();
		populateHomeBeans();
		populateLoginBeans();
	}

	private static void populateAccountDetail() {
		accountDetails.put("id", "");
		accountDetails.put("name", "Account Name");
		final Map<String, Object> formatted = new HashMap<String, Object>();
		formatted.put("currentBalance", "$99.99");
		formatted.put("availableBalance", "$99.99");
		formatted.put("term", "99 Months");
		formatted.put("apy", "99.99%");
		formatted.put("accountOpenedDate", "99/99/9999");
		formatted.put("fundingDate", "99/99/9999");
		formatted.put("lastDepositDate", "99/99/9999");
		formatted.put("lastDepositAmount", "$99.99");
		formatted.put("accruedInterest", "$99.99");
		formatted.put("interestYearToDate", "$99.99");
		formatted.put("maturityDate", "99/99/9999");
		formatted.put("minimumBalance", "$99.99");
		accountDetails.put("formatted", formatted);
	}

	private static void populateLoginBeans() {

	}

	private static void populateHomeBeans() {
		final Map<String, Object> formatted = new HashMap<String, Object>();

		final String[] colors = { "#52be7f", "#52bea8", "#3bcad3", "#36b1d9" };
		// real account values
		home.put("id", "");
		home.put("totalFunds", "$399.96");
		home.put("name", "Account Name");
		home.put("number", "***0000");
		home.put("accountName", "Account Name");
		home.put("description", "Transaction");
		home.put("color", colors[0]);

		formatted.put("currentBalance", "$99.99");
		formatted.put("postedDateMonth", "Aug");
		formatted.put("postedDateDay", "15");
		formatted.put("amount", "$109.99");

		// extra account data

		final List<Map<String, String>> accounts = new ArrayList<Map<String, String>>(NUMBER_OF_EXTRA_ACCOUNTS);

		for (int i = 1; i <= NUMBER_OF_EXTRA_ACCOUNTS; ++i) {
			final Map<String, String> account = new HashMap<String, String>(5);
			account.put("id", "");
			account.put("name", "Account Name " + i);
			account.put("number", "***000" + i);
			account.put("color", colors[i]);
			account.put("currentBalance", "$99.99");
			accounts.add(account);
		}

		home.put("accounts", accounts);

		// extra transaction data

		final List<Map<String, String>> transactions = new ArrayList<Map<String, String>>(NUMBER_OF_EXTRA_TRANSACTIONS);

		int day = NUMBER_OF_EXTRA_TRANSACTIONS + 5;
		double amount = 10.00 * NUMBER_OF_EXTRA_TRANSACTIONS + 9.99;
		for (int i = 1; i <= NUMBER_OF_EXTRA_TRANSACTIONS; ++i) {
			final Map<String, String> transaction = new HashMap<String, String>(5);
			transaction.put("postedDateMonth", "Aug");
			transaction.put("postedDateDay", String.valueOf(day--));
			transaction.put("description", "Transaction");
			transaction.put("accountName", "Account Name");
			transaction.put("amount", currencyFormat.format(amount -= 10));
			transactions.add(transaction);
		}

		home.put("transactions", transactions);

		home.put("formatted", formatted);
		final Map<String, Object> settings = new HashMap<String, Object>();
		settings.put("depositEnabled", "true");
		home.put("settings", settings);
	}

	@Override
	public Map<String, Object> getBeans(final HttpServletRequest request) {
		final AggregationState aggregationState = MgnlContext.getAggregationState();
		final String handle = aggregationState.getHandle();

		if (handle != null) {
			if (handle.endsWith("/login")) {
				return login;
			} else if (handle.endsWith("/home")) {
				return home;
			} else if (handle.endsWith("/account-detail")) {
				return accountDetails;
			}
		}
		return new HashMap<String, Object>();
	}
}